package cn.edu.tju.scs.network.ftp.utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by catold on 16/1/7.
 */
public class UserUtil {
    private volatile static UserUtil instance;

    public static UserUtil getInstance() {
        if(instance == null){
            synchronized (UserUtil.class) {
                if(instance == null){
                    instance = new UserUtil();
                }
            }
        }
        return instance;
    }

    private List<UserProfile> users;

    private UserUtil() {
        initializeUser();
    }

    /**
     * 初始化测试用户数据
     */
    private void initializeUser(){
        users = new ArrayList<UserProfile>();
        users.add(new UserProfile("1", "1"));
        users.add(new UserProfile("2", "2"));
    }

    /**
     * 判断用户名和密码是否正确
     * @param username
     * @param pwd
     * @return
     */
    public boolean checkUser(String username, String pwd) {
        boolean result = false;
        for (UserProfile userProfile : users) {
            if (userProfile.equals(new UserProfile(username, pwd))) {
                result = true;
                break;
            }
        }
        return result;
    }

    /**
     * 判断用户名和密码是否正确
     * @param userProfile
     * @return
     */
    public boolean checkUser(UserProfile userProfile) {
        boolean result = false;
        for (UserProfile temp : users) {
            if (temp.equals(userProfile)) {
                result = true;
                break;
            }
        }
        return result;
    }
}
